package net.example.activity.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;



import net.example.manager.ClientGlobalCache;
import net.example.model.GGUser;
import net.example.model.UserBasicModel;
import net.example.ggtalk.R;
import net.example.utils.HeadImgHelper;

import com.oraycn.esbasic.adapters.CommonAdapter;
import com.oraycn.esbasic.helpers.StringHelper;

import java.util.List;

public class SearchFriendAdapter extends CommonAdapter<UserBasicModel> {


   public  SearchFriendAdapter(Context context, List<UserBasicModel> datas, int item_layoutId)
   {
       super(context,datas,item_layoutId);
   }


    @Override
    protected void setConvertView(View view, UserBasicModel model,int itemViewType) {
        try{
            SearchFriendAdapter.ViewHolder holder;
            if(view.getTag()==null)
            {
                holder = new SearchFriendAdapter.ViewHolder(view);
                view.setTag(holder);
            }
            else {
                holder = (SearchFriendAdapter.ViewHolder) view.getTag();
            }
            if (model.getHeadImageUrl() == 0) {
                GGUser GGUser = ClientGlobalCache.getInstance().getUser(model.getUserID());
                HeadImgHelper.setUserHeadImg(holder.headImg, GGUser);
            } else if (model.getHeadImageUrl() > 0) {
                holder.headImg.setImageResource(model.getHeadImageUrl());
            }else {
                holder.headImg.setVisibility(View.INVISIBLE);
            }
            if(model.getUserName().equals(model.getDisplayName()))
            {
                holder.userName.setText(model.getUserName());
            }
            else {
                holder.userName.setText(model.getDisplayName()+"（"+ model.getUserName()+"）");
            }
            String catalogName= ClientGlobalCache.getInstance().getCurrentUser().getCatalogNameByFriend(model.getUserID());
            if(!StringHelper.isNullOrEmpty(catalogName))
            {
                holder.describe.setText("[ "+ catalogName+" ]");
                holder.describe.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.describe.setText("");
                holder.describe.setVisibility(View.GONE);
            }
            holder.signName.setText(model.getUserID());
        }catch (Exception ex){ex.printStackTrace();}
    }

    private class ViewHolder
    {
        public ImageView headImg;
        public TextView userName;
        public TextView describe;
        public TextView signName;

        public ViewHolder(View view)
        {
            this.headImg = (ImageView) view.findViewById(R.id.ct_photo);
            ImageView statusImg=(ImageView) view.findViewById(R.id.ct_status);
            statusImg.setVisibility(View.GONE);
            this.userName=(TextView) view.findViewById(R.id.ct_name);
            this.signName =(TextView) view.findViewById(R.id.ct_sign);
            this.describe=(TextView) view.findViewById(R.id.ct_describe);
        }
    }
}
